package com.wengu.nacos.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.wengu.nacos.bean.Movie;
import com.wengu.nacos.handler.SentinelExceptionHandler;
import com.wengu.nacos.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author LoserJohn
 * @create 2020-09-01 19:43
 */
@RestController
public class MovieController {
    @Value("${server.port}")
    private Integer port;
    @Value("${myName}")
    private String myName;
    @Value("${mysql.url}")
    private String url;
    @Value("${redis.host}")
    private String host;
    @Autowired
    MovieService movieService;

    @GetMapping("/getMovie")
    public Movie getMovie(Integer id) {
//        try {
//            TimeUnit.MILLISECONDS.sleep(200);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        int i = 1/0;
        System.out.println("当前时间为"+System.currentTimeMillis());
        return movieService.getMovie(id);
    }
    @GetMapping("/test")
    public String test(){
        return "Sentinel test...";
    }
    @GetMapping("/testHotKey")
    @SentinelResource(value = "hotKey",blockHandler = "qpcExceptionHandle",blockHandlerClass = SentinelExceptionHandler.class)
    public String testHotKey(@RequestParam(value = "p1",required= false) String p1, @RequestParam(value = "p2",required = false) String p2){
        return "testHotKey response...";
    }

    public String testHotKeyFallback(String p1,String p2){
        return "testHotKeyFallback response...";
    }
}
